{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "00d078a6-822e-4535-b7ec-6a0daa48d4da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中心标签矩阵已保存\n",
      "标签 0: 1401 个\n",
      "标签 1: 521 个\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "def get_center_labels(label_matrix, block_positions, block_size):\n",
    "    # 初始化一个空列表，用来存储每个块中心位置的标签\n",
    "    center_labels = []\n",
    "\n",
    "    # 遍历每个块的位置\n",
    "    for position in block_positions:\n",
    "        block_x, block_y = position\n",
    "        # 计算块的中心像素位置\n",
    "        center_x = block_x + (block_size // 2)\n",
    "        center_y = block_y + (block_size // 2)\n",
    "        \n",
    "        # 获取中心像素的标签\n",
    "        center_label = label_matrix[center_y, center_x]\n",
    "        center_labels.append(center_label)\n",
    "\n",
    "    # 返回一个 numpy 数组\n",
    "    return np.array(center_labels)\n",
    "\n",
    "# 选择图片和标签数据\n",
    "target = 1\n",
    "data_name = ['0618', '0854', '1066'][target - 1]\n",
    "# 1. 加载标签矩阵\n",
    "label_matrix = np.load(f'../input_data/{data_name}.npy')  # 假设标签矩阵已生成并保存在该路径\n",
    "\n",
    "# 2. 定义块大小和块的位置\n",
    "block_size = 8  # 每个块的大小，假设为 8x8\n",
    "# 假设块的位置已通过滑动窗口计算得到\n",
    "block_positions = np.array([[x * block_size, y * block_size] for x in range(31) for y in range(62)])\n",
    "\n",
    "# 3. 获取每个块的中心标签\n",
    "center_labels = get_center_labels(label_matrix, block_positions, block_size)\n",
    "\n",
    "# 4. 保存中心标签矩阵为 .npy 文件\n",
    "np.save(f'./Features/{data_name}_DictL_labels.npy', center_labels)\n",
    "\n",
    "print(\"中心标签矩阵已保存\")\n",
    "\n",
    "unique_labels, counts = np.unique(center_labels, return_counts=True)\n",
    "for label, count in zip(unique_labels, counts):\n",
    "    print(f\"标签 {label}: {count} 个\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8e5e1ef-8c15-45d4-8d03-edad82506bd6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
